package cn.charmaine.pet.service.impl;

import cn.charmaine.basic.Exception.CustomException;
import cn.charmaine.basic.util.BaiduAuditUtils;
import cn.charmaine.basic.util.LoginContext;
import cn.charmaine.basic.util.PageList;
import cn.charmaine.org.domain.Employee;
import cn.charmaine.org.domain.Logininfo;
import cn.charmaine.org.mapper.IEmployeeMapper;
import cn.charmaine.pet.domain.Pet;
import cn.charmaine.pet.domain.PetDetail;
import cn.charmaine.pet.domain.PetOnlineAuditLog;
import cn.charmaine.pet.mapper.PetDetailMapper;
import cn.charmaine.pet.mapper.PetMapper;
import cn.charmaine.pet.mapper.PetOnlineAuditLogMapper;
import cn.charmaine.pet.query.PetQuery;
import cn.charmaine.pet.service.IPetService;
import cn.charmaine.basic.base.service.impl.BaseServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xmy
 * @since 2022-07-18
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IEmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        //设置基本信息
        pet.setState(0L);
        pet.setCreatetime(new Date());
        System.out.println(pet);
        //先保存pet
        petMapper.insert(pet);
        //再保存详情
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());//得到添加宠物后的id，设置到宠物详情中去
        petDetailMapper.insert(detail);
    }

    @Override
    public void update(Pet pet) {
        //先修改pet
        petMapper.update(pet);
        //再修改详情
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.update(detail);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除详情里的数据
        petDetailMapper.deleteByPetId(id);
        //通过id删除宠物
        petMapper.deleteById(id);

    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        //先批量删除detail里的数据
        petDetailMapper.batchDeleteByPetIds(ids);
        //再删除pet里的数据
        petMapper.batchDelete(ids);
    }

    @Override
    public void auditOnsale(List<Long> ids,HttpServletRequest request) {
        //创建上架日志对象
        PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
        //利用工具类解析，得到logininfo
        Logininfo logininfo = LoginContext.getCurrentUser(request);
        //利用logininfo的id查询对应的employee
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        //设置上架日志的audit_id
        petOnlineAuditLog.setAuditId(employee.getId());

        //空参数校验
        if(StringUtils.isEmpty(ids)){
            throw new CustomException("请选择需要上架的商品！");
        }
        for (Long id : ids) {
            Pet pet = petMapper.selectById(id);
            petOnlineAuditLog.setPetId(id);
            petOnlineAuditLog.setAuditTime(new Date());
            //百度ai审核
            if(!BaiduAuditUtils.TextCensor(pet.getName())){//审核没通过
                petOnlineAuditLog.setState(0);//设置审核状态为失败
                petOnlineAuditLog.setNote("审核失败！");
                petOnlineAuditLogMapper.insert(petOnlineAuditLog);
                throw new CustomException(pet.getName()+"宠物名不符合规范，请重新输入!");
            }
            //审核成功的情况
            petOnlineAuditLog.setState(1);//设置审核状态为成功
            petOnlineAuditLog.setNote("审核成功！");
            petOnlineAuditLogMapper.insert(petOnlineAuditLog);
            //修改宠物信息
            pet.setState(1L);//设置宠物为上架
            pet.setOnsaletime(new Date());//设置宠物上架时间
            petMapper.update(pet);
        }

    }

    @Override
    public void auditOffsale(List<Long> ids,Date date) {
        if(StringUtils.isEmpty(ids)){
            throw new CustomException("请选择需要下架的商品！");
        }
        petMapper.batchUpdatePetState(ids,date);
    }

    /**
     * 店铺首页展示
     * @param query
     * @return
     */
    @Override
    public PageList<Pet> queryShopIndex(PetQuery query) {
        Long id = query.getId();
        Pet pet = petMapper.selectById(id);
        if(pet != null){
            Long shopId = pet.getShopId();
            //设置shopId
            query.setShopId(shopId);
        }
        Long count = petMapper.count(query);
        List<Pet> rows = petMapper.rows(query);
        return new PageList<Pet>(count,rows);
    }

}
